package com.tbit.main.service.impl;

import com.alibaba.fastjson.JSON;
import com.tbit.main.constant.RedisConstant;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.service.RedisService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

@Service("tokenService")
public class TokenServiceImpl implements TokenService {
    @Autowired
    private RedisService redisService;

    @Override
    public void setAccount(String token, AccountUser accountUser) {
        /**新加token,(14天有效)*/
        String userString = JSON.toJSONString(accountUser);
        redisService.add(token, userString, 7 * 24 * 3600);

        /**用户对应的token列表*/
        String userKey = RedisConstant.REDIS_ACCOUNT_USER_TOKEN_LIST + accountUser.getAccountType() + "." + accountUser.getAccountUserId();

        /**清空list，只允许一个用户登录*/
        List<String> userTokenList = new LinkedList<>();
        try {
            userTokenList = redisService.getList(userKey);
        } catch (SerializationException error) {
            redisService.del(userKey);
            LogUtil.info("删除tokenlist：" + userKey);
        }
        LogUtil.info("[" + userTokenList.size() + "]-" + userKey);
        for (String userToken : userTokenList) {
            redisService.delToList(userKey, userToken);
            redisService.del(userToken);
        }

        /**添加*/
        redisService.andToList(userKey, token);
    }

    @Override
    public AccountUser getAccount(String token) {
        String userJson = redisService.get(token);
        if (StringUtils.isBlank(userJson)) {
            return null;
        }
        try {
            return JSON.parseObject(userJson, AccountUser.class);
        } catch (SerializationException error) {
            redisService.del(token);
        }
        return null;
    }

    @Override
    public void delToken(AccountUser accountUser) {
        /**获取用户token列表*/
        String userKey = RedisConstant.REDIS_ACCOUNT_USER_TOKEN_LIST + accountUser.getAccountType() + "." + accountUser.getAccountUserId();
        List<String> tokens = new LinkedList<>();
        try {
            tokens = redisService.getList(userKey);
        } catch (SerializationException e) {
            redisService.del(userKey);
        }

        for (String token : tokens) {
            redisService.del(token);
        }
        redisService.del(userKey);
    }

    @Override
    public void refreshToken(int seconds) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getParameter("token");
        AccountUser accountUser = getAccount(token);
        redisService.add(token, JSON.toJSONString(accountUser), seconds);
    }

    @Override
    public void destroyToken() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getParameter("token");
        redisService.del(token);
    }

}
